package com.shaokel.eduservice.controller;

import com.shaokel.commonutils.ReturnResult;
import com.shaokel.eduservice.entity.EduCourse;
import com.shaokel.eduservice.entity.vo.CourseInfoVo;
import com.shaokel.eduservice.entity.vo.CoursePublishVo;
import com.shaokel.eduservice.service.EduCourseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 课程 前端控制器
 *
 * @author 少丶客
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //添加课程基本信息
    @PostMapping("/addCourseInfo")
    public ReturnResult saveCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回课程id，为了后面添加课程大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return ReturnResult.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息接口
    @GetMapping("/getCourseInfo/{courseId}")
    public ReturnResult getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return ReturnResult.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public ReturnResult updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return ReturnResult.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public ReturnResult getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo =  eduCourseService.getPublishCourseInfo(courseId);
        return ReturnResult.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("/publishCourse/{id}")
    public ReturnResult publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return ReturnResult.ok();
    }

    //课程链表，基本实现
    //TODO  完善条件查询带分页
    @GetMapping
    public ReturnResult getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return ReturnResult.ok().data("list",list);
    }

    //删除课程
    @GetMapping("/{courseId}")
    public ReturnResult deleteCourse(@PathVariable String courseId){
        eduCourseService.deleteCourse(courseId);
        return ReturnResult.ok();
    }
}

